package com.msmall.community.team.util;

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import org.apache.commons.io.FileUtils;

import com.msmall.base.util.Constants;
import com.msmall.base.util.ImageUtil;

public class TeamUtil {
	public static final String LOGO_PATH_PREFIX = "file/team/";
	public static final String LOGO_PATH_MID = "/logo/";
	public static final String LOGO_EXT = "JPEG";
	public static final String LOGO_NAME_EXT = ".jpeg";
	public static final int LOGO_CUBIC_SIZE = 100;
	/**
	 * [0] 路径 id/home/
	 * [1]名称 huangshan.jpg
	 * @param file
	 * @param sightid
	 * @param uat
	 * @return
	 */
	public static String[] saveImage(File file, String filename, Long userid, String uat, Long teamid){
		String logoName = filename.substring(0,filename.indexOf("."));
		File target = new File(getAbsoluteHomePath(userid,uat, teamid)+logoName + LOGO_NAME_EXT);
		try {
			File tFolder = new File(getAbsoluteHomePath(userid, uat, teamid));
			FileUtils.deleteDirectory(tFolder);
			tFolder.mkdirs();
			BufferedImage srcImage = ImageIO.read(file);
			srcImage = ImageUtil.scale(srcImage, LOGO_CUBIC_SIZE);
			ImageIO.write(srcImage, LOGO_EXT, target);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return new String[]{getHomePath(uat, teamid), logoName + LOGO_NAME_EXT};
	}
	
	public static String getAbsoluteHomePath(Long sightid, String uat, Long teamid){
		return Constants.ROOTPATH + getHomePath(uat, teamid);
	}
	
	public static String getHomePath(String uat, Long teamid){
		return LOGO_PATH_PREFIX + teamid + LOGO_PATH_MID;
	}
}
